package com.chen.pspms.controller;

import com.chen.pspms.entity.Resourcedemand;
import com.chen.pspms.entity.Standardizationtable;
import com.chen.pspms.service.ResourcedemandService;
import com.chen.pspms.service.StandardizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/standardization")
public class StandardizationController {
    @Autowired
    StandardizationService standardizationService;
    @Autowired
    ResourcedemandService resourcedemandService;

    @RequestMapping("/list")
    public String findAll(Model model){
        List<Standardizationtable> standardizationtablelist = standardizationService.findyAll();
        model.addAttribute("standardizationtablelist",standardizationtablelist);
        return "生产标准化管理/生产标准化管理页面";
    }

    @RequestMapping("/delete")
    public String delete(Integer id) {
        Standardizationtable standardizationtable=standardizationService.findyById(id);
        Resourcedemand resourcedemand=resourcedemandService.findyByType(standardizationtable.getHazardtype());
        standardizationService.delete(id);
        resourcedemandService.delete(resourcedemand.getId());
        return "redirect:/standardization/list";
    }

    @RequestMapping("/show")
    public String show(Integer id, Model model){
        //调用业务处理
        if(id==0) {
            //如果是新增，直接返回新增页面
            return "生产标准化管理/新建";
        }else {
            //如果是修改，查询数据后返回
            Standardizationtable standardizationtable = standardizationService.findyById(id);
            Resourcedemand resourcedemand=resourcedemandService.findyByType(standardizationtable.getHazardtype());
            model.addAttribute("standardizationtable", standardizationtable);
            model.addAttribute("resourcedemand", resourcedemand);
        }
        //处理业务返回结果
        return "生产标准化管理/修改";
    }

    @RequestMapping("/save")
    public String save(Standardizationtable standardizationtable, Resourcedemand resourcedemand, Model model) {
        //调用业务处理
        String exist =null;
        if(standardizationService.findyByType(standardizationtable.getHazardtype())!=null) {
            //如果员工合同表已存在
            exist="该生产标准已存在";
        }else {
            //合同不存在，则添加
            resourcedemand.setStandard(standardizationtable.getHazardtype());
            standardizationService.save(standardizationtable);
            resourcedemandService.save(resourcedemand);
        }
        //处理业务返回结果
        List<Standardizationtable> standardizationtablelist = standardizationService.findyAll();
        model.addAttribute("standardizationtablelist", standardizationtablelist);
        model.addAttribute("exist", exist);
        return "生产标准化管理/生产标准化管理页面";
    }

    @RequestMapping("/update")
    public String update(Standardizationtable standardizationtable,Resourcedemand resourcedemand, Model model) {
        //调用业务处理
        standardizationService.update(standardizationtable);
        resourcedemandService.update(resourcedemand);
        //处理业务返回结果
        return "redirect:/standardization/list";//重定向就是在字符串前面加上前�? redirect:
    }
}
